
package org.upt.dcda.presentation;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.upt.dcda.BookManagerBeanRemote;

/**
 * Servlet implementation class BookModifyServlet
 */
@WebServlet("/BookModifyServlet")
public class BookModifyServlet extends HttpServlet
{

	private static final long		serialVersionUID	= 1L;

	@EJB
	private BookManagerBeanRemote	bookManagerBean;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookModifyServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		try
		{
			if (SessionAttributes.isAdmin(request))
			{
				int bookId = Integer.valueOf(request.getParameter("bookId"));
				String title = request.getParameter("title");
				String description = request.getParameter("description");
				int price = Integer.valueOf(request.getParameter("price"));
				int authorId = Integer.valueOf(request.getParameter("authors"));

				this.bookManagerBean.modifyBook(bookId, title, description, authorId, price);
				response.sendRedirect(request.getContextPath() + "/index.jsp");
			}
			else
			{
				response.sendRedirect(request.getContextPath() + "/login.jsp");
			}
		}
		catch (Throwable t)
		{
			getServletContext().log("Caught Exception: " + t, t);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.toString());
		}
	}

}
